﻿using System;

namespace diantou.web.user
{
    /// <summary>
    /// 用户视图
    /// </summary>
    /// <typeparam name="viewType"></typeparam>
    class view<viewType> : web.view<viewType>
        where viewType : view<viewType>
    {
        /// <summary>
        /// 用户
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        protected diantou.dataAccess.user user;
        /// <summary>
        /// 是否自己
        /// </summary>
        protected bool isSelf
        {
            get
            {
                return user.Id == currentUserId;
            }
        }
        /// <summary>
        /// 用户的关注者集合
        /// </summary>
        protected focusImage focusAnyImage
        {
            get
            {
                if (requestHeader.IsReView) return new focusImage { ViewOnly = true };
                int count;
                return new focusImage(diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.focusUser.getUserIds(user.Id, diantou.dataAccess.focusUser.FocusTypes, 4 * 10, 1, out count)), 10);
            }
        }
        /// <summary>
        /// 用户关注的用户集合
        /// </summary>
        protected focusImage focusUserAnyImage
        {
            get
            {
                if (requestHeader.IsReView) return new focusImage { ViewOnly = true };
                int count;
                return new focusImage(diantou.dataAccess.clientCache.User.Get(diantou.dataAccess.tcpCall.focusUser.getFocusIds(user.Id, diantou.dataAccess.focusUser.FocusTypes, 4 * 10, 1, out count)), 10);
            }
        }
        /// <summary>
        /// 用户关系
        /// </summary>
        protected userRelation userRelation
        {
            get { return new userRelation { User = user, UserId = currentUserId }; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        protected virtual bool loadView(int userId)
        {
            return load(userId);
        }
        /// <summary>
        /// 加载视图
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="searchEnginePath">搜索引擎重定向</param>
        /// <returns></returns>
        protected bool load(int userId, string searchEnginePath = null)
        {
            if (userId == 0)
            {
                checkSearchEngine(searchEnginePath, webConfig.NotFound404);
                return false;
            }
            if (userId == currentUserId) user = currentUser;
            else user = diantou.dataAccess.clientCache.User[userId];
            if (user != null) return true;
            AjaxResponse(webConfig.NotFound404);
            return false;
        }
    }
}
